package com.stox.shared.util;

import java.util.Comparator;

import com.stox.shared.model.intf.Nameable;

public class NameableComparator<T extends Nameable> implements Comparator<T> {

	private static  NameableComparator<?> instance;
	
	@SuppressWarnings("unchecked")
	public  static <V extends Nameable> NameableComparator<V> getInstance(){
		if(null == instance){
			instance = new NameableComparator<>();
		}
		return (NameableComparator<V>) instance;
	}
	
	private NameableComparator() {
		
	}
	
	@Override
	public int compare(Nameable o1, Nameable o2) {
		if(null == o1 && null != o2){
			return -1;
		}else if(null != o1 && null == o2){
			return 1;
		}else if(o1 != null && o2 != null){
			if(null == o1.getName() && null != o2.getName()){
				return -1;
			}else if(null != o1.getName() && null == o2.getName()){
				return 1;
			}else{
				return o1.getName().trim().compareToIgnoreCase(o2.getName().trim());
			}
		}
		return 0;
	}
	
}
